gdkwindow: avoid multiple walking of children list
authorChristian Hergert <christian@hergert.me>
Mon, 14 Sep 2015 19:25:44 +0000 (12:25 -0700)
committerChristian Hergert <christian@hergert.me>
Mon, 14 Sep 2015 19:40:12 +0000 (12:40 -0700)
This counts the number of children and fetches the last GList
node at the same time.

gdk/gdkwindow.c

index 3298b8a58427883bb4f5ede775ceb4ec71465a95..89ed898c84b01c501033c5813b547f7515f5f677 100644 (file)
@@ -3545,6 +3545,7 @@ _gdk_window_process_updates_recurse_helper (GdkWindow *window,
   GdkWindow **free_children = NULL;
   int i, n_children;
   GList *l;
+  GList *last_link;
 
   if (window->destroyed)
     return;
@@ -3587,14 +3588,22 @@ _gdk_window_process_updates_recurse_helper (GdkWindow *window,
       _gdk_event_emit (&event);
     }
 
-  n_children = g_list_length (window->children);
+  n_children = 0;
+  last_link = NULL;
+  /* Count n_children and fetch bottommost at same time */
+  for (l = window->children; l != NULL; l = l->next)
+    {
+      last_link = l;
+      n_children++;
+    }
+
   children = g_newa (GdkWindow *, n_children);
   if (children == NULL)
     children = free_children = g_new (GdkWindow *, n_children);
 
   n_children = 0;
   /* Iterate over children, starting at bottommost */
-  for (l = g_list_last (window->children); l != NULL; l = l->prev)
+  for (l = last_link; l != NULL; l = l->prev)
     {
       child = l->data;